package org.neu.ccs.secureim;

import java.io.Console;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Properties;
import java.util.StringTokenizer;
import java.util.Vector;

import org.apache.commons.codec.binary.Base64;

/*
 * The main class to start the client application.
 */

public class ClientMain {

	public static void main(String[] args) {

		//Use console object for user i/o because it allows password hiding
		Console con = System.console();
		if(con == null )
		{
			System.out.println("Error retrieving console object.");
			System.exit(-1);
		}

		//Read connection defaults from properties file
		Properties prop = new Properties();
		try
		{
			FileInputStream fin = new FileInputStream("ClientSettings.properties");
			prop.load(fin);
		}
		catch(IOException e)
		{
			System.out.println("Error reading settings file.");
			e.printStackTrace();
			System.exit(-1);
		}

		if( !(prop.containsKey("ServerAddress") && prop.containsKey("ServerPort") && prop.containsKey("ServerPubKey")) )
		{
			System.out.println("Properties file does not contain server settings.");
			System.exit(-1);
		}

		Base64 enc = new Base64();

		byte[] serverPK = null;
		serverPK = enc.decode((String)prop.get("ServerPubKey"));


		InetAddress serverAddr = null;
		int serverPort = Integer.parseInt(prop.getProperty("ServerPort"));
		try {
			serverAddr = InetAddress.getByName((String) prop.get("ServerAddress"));
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		con.printf("Username: ");
		String user = con.readLine();
		con.printf("Password:");
		char[] pass = con.readPassword();

		ChatClient client = new ChatClient(user, pass, serverAddr, serverPort, serverPK);
		//client.testConnection();

		try
		{
			client.start();
		}
		catch(Exception e)
		{
			System.out.println("Error starting listener thread.");
			return;
		}
		
		if( client.login() )
		{
			System.out.print(">");
			String input = con.readLine();
			while( !input.toLowerCase().equals( "exit") )
			{
				try
				{
					StringTokenizer t = new StringTokenizer(input);
					String command = t.nextToken();
					if( command.toLowerCase().equals("list"))
					{
						Vector<String> users = client.list();
						if( users != null )
						{
							System.out.println("Users online:");
							for( int x = 0; x < users.size(); x++ )
								System.out.println(users.get(x));
						}
					}
					else if( command.toLowerCase().equals("locate"))
					{
						String otherUser = t.nextToken();
						UserInfo inf = client.locate(otherUser);
						System.out.println("IP Address: " + inf.getLocation());
						System.out.println("Port: " + inf.getPort());
					}
					else if( command.toLowerCase().equals("send") )
					{
						String otherUser = t.nextToken();
						String message = "";
						while( t.hasMoreTokens() )
							message = message + " " + t.nextToken() ;

						client.send(otherUser, message);
					}
				}
				catch(SocketException e)
				{
					System.out.println("Too many connections from this host address, request dropped");
				}
				catch( Exception e )
				{
					e.printStackTrace();
				}
				System.out.print(">");
				input = con.readLine();
			}
			client.logout();
		}
	}



}
